﻿using System;
using System.Collections;
using System.Data;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using AMS.Inventory.PDA.Castle.DataAccess;

namespace AMS.Inventory.PDA.UserControls
{
    public partial class UsrCotrlComBox : UserControl
    {
        private readonly Hashtable _hashtable;
        private ListBox.ObjectCollection _arrayCheck;
        private FrmTypename _frmTypename;
        private string _strFileFullName;
        private DataTable _typeDt;
        public string m_ClassName;
        public SqliteDBServer m_SqlDb;
        public string m_TypeName;

        public UsrCotrlComBox()
        {
            if (m_SqlDb == null)
                m_SqlDb = new SqliteDBServer();
            _hashtable = clsLocalization.ReadContectResource("UsrCotrlComBox", "ZH");
            InitializeComponent();
        }

        public string ClassName
        {
            get { return m_ClassName; }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    m_ClassName = value;
                }
            }
        }

        public override Color BackColor
        {
            get { return cboType.BackColor; }
            set { cboType.BackColor = value; }
        }

        public string TypeName
        {
            get { return m_TypeName; }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    m_TypeName = value;
                }
            }
        }

        public new string Text
        {
            get
            {
                cboType.BackColor = Color.Transparent;
                string txtType = cboType.Text.Trim();
                int selectIndex = cboType.Items.IndexOf(txtType);
                if (!string.IsNullOrEmpty(txtType))
                {
                    if (selectIndex > -1)
                    {
                        cboType.SelectedIndex = selectIndex;
                    }
                    else
                    {
                        if (_typeDt.Columns.Count < 1)
                        {
                            _typeDt.Columns.Add("KeyVal", typeof (string));
                            _typeDt.Columns.Add("KeyVal2", typeof (string));
                            _typeDt.Columns.Add("KeyVal3", typeof (string));
                        }
                        if (cboType.Items.Contains(txtType) )//..Select("KeyVal='" + txtType + "'").Length < 0
                        {
                            if (!getSelectSql(m_ClassName, m_TypeName, txtType))
                            {
                                if (DialogResult.No ==
                                    MessageBox.Show(
                                        txtType + ":" + clsLocalization.getFromContentNames(_hashtable, "IsAddType"),
                                        ModCommon.MagboxTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Question,
                                        MessageBoxDefaultButton.Button1))
                                {
                                    cboType.BackColor = Color.Red;
                                    return cboType.Text;
                                }
                                else
                                {
                                    cboType.Text = txtType;
                                }
                            }
                        }
                        DataRow row = _typeDt.NewRow();
                        row["KeyVal"] = txtType;
                        _typeDt.Rows.Add(row);
                        WriteXml(_typeDt);
                        if (!cboType.Items.Contains(txtType))
                        {
                            cboType.Items.Add(txtType);
                            selectIndex = cboType.Items.IndexOf(txtType);
                            if (selectIndex > -1)
                            {
                                cboType.SelectedIndex = selectIndex;
                            }
                        }
                    }
                }
                return cboType.Text;
            }
            set
            {
                cboType.BackColor = Color.Transparent;
                SetItems();
                int selectIndex = cboType.Items.IndexOf(value);
                if (selectIndex > -1)
                {
                    cboType.SelectedIndex = selectIndex;
                }
                else
                {
                    if (string.IsNullOrEmpty(value))
                        return;
                    if (_typeDt.Columns.Count < 1)
                    {
                        _typeDt.Columns.Add("KeyVal", typeof (string));
                        _typeDt.Columns.Add("KeyVal2", typeof (string));
                        _typeDt.Columns.Add("KeyVal3", typeof (string));
                    }
                    if (_typeDt.Select("KeyVal='" + value + "'").Length < 0)
                    {
                        if (!getSelectSql(m_ClassName, m_TypeName, value))
                        {
                            if (DialogResult.No ==
                                MessageBox.Show(value + clsLocalization.getFromContentNames(_hashtable, "IsAddType"),
                                                ModCommon.MagboxTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Question,
                                                MessageBoxDefaultButton.Button1))
                            {
                                cboType.BackColor = Color.Red;
                                return;
                            }
                            else
                            {
                                cboType.Text = value;
                            }
                        }
                    }
                    cboType.BackColor = Color.Transparent;
                    DataRow row = _typeDt.NewRow();
                    row["KeyVal"] = value;
                    _typeDt.Rows.Add(row);
                    WriteXml(_typeDt);
                    if (!cboType.Items.Contains(value))
                    {
                        cboType.Items.Add(value);
                        selectIndex = cboType.Items.IndexOf(value);
                        if (selectIndex > -1)
                        {
                            cboType.SelectedIndex = selectIndex;
                        }
                    }
                }
            }
        }

        public new long Width
        {
            get { return Width; }
            set
            {
                Width = value;
                cboType.Width = Convert.ToInt32(Width) - btnReview.Width;
                cboType.Top = Top;
                btnReview.Top = Top;
                cboType.Left = Left;
                btnReview.Left = Left + cboType.Width;
            }
        }

        private void BtnReviewClick(object sender, EventArgs e)
        {
            _frmTypename = new FrmTypename(_typeDt, m_ClassName, m_TypeName, m_SqlDb);
            _frmTypename.ShowDialog();
            _arrayCheck = _frmTypename._arrayCheck;
            string containStr = "";
            foreach (object list in _arrayCheck)
            {
                containStr += "'" + list + "',";
            }
            WriteXml(containStr);
            SetItems();
        }

        private void SetItems()
        {
            _strFileFullName = ModCommon.PrjPath + "\\BaseData\\" + m_ClassName + "_" + m_TypeName + ".xml";
            _typeDt = new DataTable(m_ClassName + m_TypeName);
            if (File.Exists(_strFileFullName))
                _typeDt.ReadXml(_strFileFullName);
            else
            {
            }
            cboType.Items.Clear();
            if (_typeDt != null && _typeDt.Rows.Count > 0)
            {
                foreach (DataRow row in _typeDt.Rows)
                {
                    string strKeyVal = Convert.ToString(row[0]);
                    cboType.Items.Add(strKeyVal);
                }
            }
        }

        /// <summary>
        /// 写XML
        /// </summary>
        /// <param name="containStr"></param>
        private void WriteXml(string containStr)
        {
            string strDir = ModCommon.PrjPath + "\\BaseData";
            if (!Directory.Exists(strDir))
                Directory.CreateDirectory(strDir);
            _strFileFullName = strDir + "\\" + m_ClassName + "_" + m_TypeName + ".xml";
            if (File.Exists(_strFileFullName))
                File.Delete(_strFileFullName);
            _typeDt.TableName = m_ClassName + m_TypeName;
            _typeDt.WriteXml(_strFileFullName, XmlWriteMode.WriteSchema);
        }

        /// <summary>
        /// 重载写XML 根据Table写XML
        /// </summary>
        /// <param name="typeDt"></param>
        private void WriteXml(DataTable typeDt)
        {
            string strDir = ModCommon.PrjPath + "\\BaseData";
            if (!Directory.Exists(strDir))
                Directory.CreateDirectory(strDir);
            _strFileFullName = strDir + "\\" + m_ClassName + "_" + m_TypeName + ".xml";
            if (File.Exists(_strFileFullName))
                File.Delete(_strFileFullName);
            typeDt.TableName = m_ClassName + m_TypeName;
            typeDt.WriteXml(_strFileFullName, XmlWriteMode.WriteSchema);
        }

        /// <summary>
        /// 查询出要写入Xml的数据
        /// </summary>
        /// <param name="containStr"></param>
        /// <returns></returns>
        private DataTable GetTableByCon(string containStr)
        {
            string sql;
            sql = _frmTypename.getSelectSql(m_ClassName, m_TypeName);
            if (!string.IsNullOrEmpty(containStr))
            {
                sql = sql + " and KeyVal in( " + containStr.Substring(0, containStr.Length - 1) + ")";
            }
            else
            {
                sql = sql + "and  1=2";
            }
            DataTable typeDt = null;
            try
            {
                typeDt = m_SqlDb.GetDataTable(sql);
            }
            finally
            {
                m_SqlDb.ConnectedDbClose();
            }

            return typeDt;
        }

        /// <summary>
        /// 查询数据库中是否存在指定属性
        /// </summary>
        /// <param name="strClassName"></param>
        /// <param name="strTypeName"></param>
        /// <returns></returns>
        public bool getSelectSql(string strClassName, string strTypeName, string keyVal)
        {
            string sql;
            DataTable dt;
            bool isExist = false;
            sql = "SELECT distinct keyVal,keyVal2,keyVal3 FROM AmsInventory_BaseDataInfo ";
            if (string.IsNullOrEmpty(strClassName))
            {
                if (!string.IsNullOrEmpty(strTypeName))
                {
                    sql = sql + "where  TypeName='" + strTypeName + "'";
                }
                else
                {
                    sql = sql + " where ClassName='" + strClassName +
                          "' and TypeName='" + strTypeName + "'";
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(strTypeName))
                {
                    sql = sql + " where ClassName='" + strClassName + "' and TypeName='" + strTypeName + "'";
                }
                else
                {
                    sql = sql + " where ClassName='" + strClassName + "'";
                }
            }
            sql = sql + " and keyVal='" + keyVal + "'";
            dt = m_SqlDb.GetDataTable(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                isExist = true;
            }
            return isExist;
        }
    }
}